<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Document</title>
</head>
<body>


<script>
  // 65X53X15=3只
  // 75X46X32=3只

  const data = [{aaLength:74,aaWidth:49,productNum:3},{aaLength:75,aaWidth:47,productNum:3},{aaLength:71,aaWidth:46,productNum:3},{aaLength:75,aaWidth:43,productNum:3}]

  function findSimilarItems(data) {
    // 创建深拷贝
    const result = data.map(item => ({...item}));

    // 按 productNum 分组
    const groups = {};
    result.forEach(item => {
      if (!groups[item.productNum]) {
        groups[item.productNum] = [];
      }
      groups[item.productNum].push(item);
    });

    // 检查每个分组中的元素
    Object.values(groups).forEach(group => {
      // 收集所有相近的值
      const similarGroups = new Map();

      // 第一次遍历：收集相近的值
      for (let i = 0; i < group.length; i++) {
        for (let j = i + 1; j < group.length; j++) {
          const item1 = group[i];
          const item2 = group[j];

          // 比较所有可能的组合
          const comparisons = [
            { val1: item1.aaLength, val2: item2.aaLength, type1: 'aaLength', type2: 'aaLength' },
            { val1: item1.aaLength, val2: item2.aaWidth, type1: 'aaLength', type2: 'aaWidth' },
            { val1: item1.aaWidth, val2: item2.aaLength, type1: 'aaWidth', type2: 'aaLength' },
            { val1: item1.aaWidth, val2: item2.aaWidth, type1: 'aaWidth', type2: 'aaWidth' }
          ];

          comparisons.forEach(comp => {
            const diff = Math.abs(comp.val1 - comp.val2);
            if (diff <= 20) {
              const key = `${comp.type1}-${comp.type2}-${Math.min(comp.val1, comp.val2)}`;
              if (!similarGroups.has(key)) {
                similarGroups.set(key, {
                  items: [],
                  type1: comp.type1,
                  type2: comp.type2,
                  values: []
                });
              }
              similarGroups.get(key).items.push(
                { item: item1, value: comp.val1, type: comp.type1 },
                { item: item2, value: comp.val2, type: comp.type2 }
              );
              similarGroups.get(key).values.push(comp.val1, comp.val2);
            }
          });
        }
      }

      // 第二次遍历：应用平均值
      for (const [_, group] of similarGroups) {
        const uniqueItems = [...new Set(group.items.map(x => x.item))];
        if (uniqueItems.length > 1) {
          const avgValue = Math.round(group.values.reduce((sum, val) => sum + val, 0) / group.values.length);

          // 更新所有相关项
          uniqueItems.forEach(item => {
            // 检查并更新aaLength
            if (group.items.some(x => x.item === item && x.type === 'aaLength')) {
              item.aaLength = avgValue;
            }
            // 检查并更新aaWidth
            if (group.items.some(x => x.item === item && x.type === 'aaWidth')) {
              item.aaWidth = avgValue;
            }
          });
        }
      }
    });

    return result;
  }

  // 测试数据
  const testData = [
    { aaLength: 720, aaWidth: 1, productNum: 3 },
    { aaLength: 730, aaWidth: 1, productNum: 3 },
    { aaLength: 1, aaWidth: 1, productNum: 3 },
    { aaLength: 1, aaWidth: 1, productNum: 3 }
  ];

  // 测试函数
  console.log('原始数据：', JSON.parse(JSON.stringify(testData))); // 使用深拷贝打印原始数据
  const updatedItems = findSimilarItems(testData);
  console.log('更新后的数据：', updatedItems);
</script>


</body>
</html>
